package  
{
	import events.LoaderEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	import util.DataSender;
	/**
	 * ...
	 * @author Kasoi
	 */
	public class Texts extends EventDispatcher
	{
		[Event(name = "loadComplete", type = "events.LoaderEvent")]
		
		private const TEXTS: String = 'texts';
		
		////////////////////////////////////////////////////////////////////
		// Lazy Singleton
		////////////////////////////////////////////////////////////////////
		
		static public var instance: Texts;
		
		static private var textsIsInitialized: Boolean = false;
		
		public function Texts() 
		{
			if (Texts.textsIsInitialized) throw new Error('Texts already initialized');
			Texts.textsIsInitialized = true;
			Texts.dictionary = new Dictionary();
		}
		
		
		
		static public function getText(code: String) : String
		{
			var text: String = Texts.dictionary[code];
			if (text == null) text = '!' + code;
			return text;
		}
		
		static private var dictionary: Dictionary;
		
		////////////////////////////////////////////////////////////////////
		// Private properties
		////////////////////////////////////////////////////////////////////
		
		private var textsIsLoading: Boolean = false;
		
		private var sender: DataSender;
		
		private var loader: URLLoader;
		
		////////////////////////////////////////////////////////////////////
		// Public methods
		////////////////////////////////////////////////////////////////////	
				
		
		public function loadTexts() : void 
		{
			if(this.textsIsLoading) throw new Error("Texts already loading");
			this.textsIsLoading = true;
			
			this.loader = new URLLoader(new URLRequest(Settings.TEXTS_PATH));
			this.loader.addEventListener(Event.COMPLETE, this.loader_onLoad);
		}
		
		public function addTexts(code: String, value: String) : void 
		{
			Texts.dictionary[code] = value;
		}
		
		////////////////////////////////////////////////////////////////////
		// Private methods
		////////////////////////////////////////////////////////////////////
		
		private function parseTextsBase(base: XML) : void 
		{
			var textLength: Number = base.text.length();
			
			for (var i: int = 0; i < textLength; i++) 
			{
				var text: XML = base.text[i];
				this.addTexts(text.@code, text);
			}
		}
		
		////////////////////////////////////////////////////////////////////
		// Listeners
		////////////////////////////////////////////////////////////////////
		
		private function sender_onLoadTexts(data: XML) : void 
		{
			//Log.data(data);
			var baseLength: Number = data.base.length();
			for (var i: int = 0; i < baseLength; i++) 
			{
				var base: XML = data.base[i];
				if (base.@name == this.TEXTS) this.parseTextsBase(base);
			}
			
			this.dispatchEvent(new LoaderEvent(LoaderEvent.LOAD_COMPLETE));
		}
		
		private function loader_onLoad(e: Event) : void 
		{
			var data: XML = new XML(this.loader.data);
			this.sender_onLoadTexts(data);
		}
		
	}

}